﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using Newtonsoft.Json;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Local.Editor.DataTableTools
{
    public sealed class DataTableGeneratorMenu
    {
        [MenuItem("DataTable/Generate DataTables")]
        public static void GenerateDataTables()
        {
            var setting = JsonConvert.DeserializeObject<DateTableSetting>(File.ReadAllText(Path.Combine("Assets/Local/Configs", "DateTableSetting.json")));
            if (setting != null)
            {
                foreach (string dataTableName in setting.m_Tables)
                {
                    DataTableProcessor dataTableProcessor = DataTableGenerator.CreateDataTableProcessor(dataTableName);
                    if (!DataTableGenerator.CheckRawData(dataTableProcessor, dataTableName))
                    {
                        Debug.LogError(Utility.Text.Format("Check raw data failure. DataTableName='{0}'", dataTableName));
                        break;
                    }

                    DataTableGenerator.GenerateDataFile(dataTableProcessor, dataTableName);
                    DataTableGenerator.GenerateCodeFile(dataTableProcessor, dataTableName);
                }
                AssetDatabase.Refresh();
            }            
            
        }
    }
}
